﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace WpfApplication1
{
    public static class PdfToJpg
    {
        public static List<string> Resolve(string fileName, string tempDirectory)
        {
            List<string> list = new List<string>();
            string fileCopy = string.Concat(new object[]
            {
                tempDirectory,
                "\\",
                Guid.NewGuid(),
                Path.GetExtension(fileName)
            });
            File.Copy(fileName, fileCopy);
            fileName = fileCopy;

            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
            string jpgTempDir = string.Concat(new object[]
            {
                tempDirectory,
                "\\",
                Guid.NewGuid(),
                "\\"
            });

            Directory.CreateDirectory(jpgTempDir);
            string jpgTempFileName = jpgTempDir + fileNameWithoutExtension + "-%08d.jpg";
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "PDF"),
                Arguments = string.Concat(new string[]
                {
                    "-dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4 -sOutputFile=\"",
                    jpgTempFileName,
                    "\" \"",
                    fileName,
                    "\""
                }),
                FileName = "gswin32.exe"
            };
            Process process = new Process
            {
                StartInfo = startInfo
            };
            process.Start();
            process.WaitForExit(2147483647);
            ArrayList arrayList = new ArrayList();
            ArrayList arrDirs = new ArrayList();
            GetFileList(jpgTempDir, arrDirs, arrayList);
            list.AddRange(from object file in arrayList
                          select file.ToString());
            return list;
        }

        public static void GetFileList(string filePath, ArrayList arrDirs, ArrayList arrFiles)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            string[] files = Directory.GetFiles(filePath);
            string[] directories = Directory.GetDirectories(filePath);
            string[] array = files;
            foreach (string value in array)
            {
                arrFiles.Add(value);
            }
            string[] array2 = directories;
            foreach (string value2 in array2)
            {
                arrDirs.Add(value2);
            }
            if (directories.Length <= 0)
            {
                return;
            }
            string[] array3 = directories;
            foreach (string filePath2 in array3)
            {
                GetFileList(filePath2, arrDirs, arrFiles);
            }
        }
    }
}
